package com.contract.spirit.ui.exchange.bind

import android.content.Context
import android.content.Intent
import androidx.core.view.isGone
import com.contract.commonlib.util.ext.setOnSingleClickListener
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.util.CommonUtils
import com.contract.spirit.R
import com.contract.spirit.bean.entity.DialogBean
import com.contract.spirit.config.Constant
import com.contract.spirit.config.ExchangeType
import com.contract.spirit.databinding.ActivityBindExchangeBinding
import com.contract.spirit.func.dialog.CommonRuleTipsDialog
import com.contract.spirit.func.dialog.base.BasePopDataBindImpl.OnDialogCallBack
import com.contract.spirit.manage.CommonConfigManager
import com.contract.spirit.ui.order.MyOrderActivity
import com.contract.spirit.ui.webview.CommonWebViewActivity

/**
 * @author: witness
 * created: 2021/9/26
 * desc:
 */
class BindExchangeActivity : BaseActivity<ActivityBindExchangeBinding, BindExchangeViewModel>() {
    override fun setContentView(): Int {
        return R.layout.activity_bind_exchange
    }

    override fun initData() {
        mViewModel.exchangeType = intent.getStringExtra(Constant.EXCHANGE_TYPE)
        mBinding.model = mViewModel
        val bianApiStudyUrl = CommonConfigManager.configInfoResponse.bindBianApiStudyUrl
        val okexApiStudyUrl = CommonConfigManager.configInfoResponse.bindOkexApiStudyUrl
        mBinding.apply {
            etPassphrase.isGone = ExchangeType.OKEX != mViewModel.exchangeType
            btnBind.setOnSingleClickListener {
                showBindTipsDialog()
            }
            tvTeach.setOnSingleClickListener {
                CommonWebViewActivity.goLaunch(
                    mContext, "Api获取教程",
                    if (ExchangeType.OKEX == mViewModel.exchangeType) okexApiStudyUrl else bianApiStudyUrl
                )
            }
        }
    }

    private var bindExchangeTipDialog: CommonRuleTipsDialog? = null
    private fun showBindTipsDialog() {
        if (CommonUtils.isPopShow(bindExchangeTipDialog)) {
            return
        }
        val dialogBean = DialogBean()
        dialogBean.apply {
            isShowClose = false
            leftBtnText = "取消"
            rightBtnText = "接入"
            subTitle = "接入合约精灵实盘的API仅需只读权限，无需交易或提现权限，请您确认后继续接入。"
            title = "免责申明"
            bindExchangeTipDialog =
                CommonRuleTipsDialog.show(mContext, this, object : OnDialogCallBack {
                    override fun onRightBtnClick() {
                        bindExchangeTipDialog!!.dismiss()
                        startBind()
                    }

                    override fun onLeftBtnClick() {
                        bindExchangeTipDialog!!.dismiss()
                    }
                })
        }
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.bindResponse.observe(this, {
            parseState(it, {
                MyOrderActivity.launch(mContext, mViewModel.exchangeType!!)
                finish()
            })
        })
    }

    private fun startBind() {
        mViewModel.bindApi()
    }

    companion object {
        fun launch(context: Context, exchangeType: String) {
            val intent = Intent(context, BindExchangeActivity::class.java)
            intent.putExtra(Constant.EXCHANGE_TYPE, exchangeType)
            context.startActivity(intent)
        }
    }
}